package cn.hx.handsetticketinspection.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.HashMap;

import cn.hx.handsetticketinspection.R;
import cn.hx.handsetticketinspection.utils.BVUtils;

public class EmpLoginActivity extends Activity {
    private static final int START_SCAN = 0x0001;
    private EditText mEmpNo;
    private EditText mEmpPassword;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action_emp_login);
        initView();
    }

    private void initView() {
        mEmpNo = (EditText) findViewById(R.id.emp_no);
        mEmpPassword = (EditText) findViewById(R.id.emp_password);
    }

    public void login(View view) {
        System.out.println(view.getId());
        System.out.println("编号:" + mEmpNo.getEditableText().toString() + ", 密码:" + mEmpPassword.getEditableText().toString());
        String empNo = mEmpNo.getEditableText().toString();
        String empPassword = mEmpPassword.getEditableText().toString();
        if (empNo == null || "".equals(empNo)) {
            Log.i("登录失败", "员工编号不能空");
            return;
        }
        if (empPassword == null || "".equals(empPassword)) {
            Log.i("登录失败", "员工编号不能空");
            return;
        }
        Intent intent = new Intent();
        intent.setAction("com.sunmi.scan");
        intent.setPackage("cn.hx.handsetticketinspection");
        startActivityForResult(intent, START_SCAN);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == START_SCAN && data != null) {
            Bundle bundle = data.getExtras();
            ArrayList<HashMap<String, String>> result = (ArrayList<HashMap<String, String>>) bundle.getSerializable("data");
            // StartActivity.MyAdapter myAdapter = new StartActivity.MyAdapter(this, result);
            System.out.println(result);

            if (result!=null && result.size()>0) {
                new AlertDialog.Builder(this).setTitle("二维码信息").setMessage(result.get(0).toString())
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent();
                                intent.setAction("com.sunmi.scan");
                                intent.setPackage("cn.hx.handsetticketinspection");
                                startActivityForResult(intent, START_SCAN);
                            }
                        }).show();
            }else{
                new AlertDialog.Builder(this).setTitle("二维码信息").setMessage("没有二维码信息")
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent();
                                intent.setAction("com.sunmi.scan");
                                intent.setPackage("cn.hx.handsetticketinspection");
                                startActivityForResult(intent, START_SCAN);
                            }
                        }).show();
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
